<?php
namespace app\api\controller;
use think\Db;
use app\admin\model\Config;


class Course extends App
{
    
    public static function WhiteList()
    {
        return [
           
            'get_subject_list',
			
        ];
    }
    
	public function get_grade_list()
    {
		$list = Db::name('course_grade')->field('id,title')->where('status',1)->order('sort asc,id asc')->select();
		return $this->show(1,"success", $list);
	}
	
	public function get_subject_list()
    {
		$list = Db::name('course_subject')->field('id,title')->where('status',1)->order('sort asc,id asc')->select();
		return $this->show(1,"success", $list);
	}
	public function get_version_list()
    {
		$list = Db::name('course_version')->field('id,title')->where('status',1)->order('sort asc,id asc')->select();
		return $this->show(1,"success", $list);
	}
	
    public function index()
    {
        $pagesize = 10;
        $page = $this->request->get("page",0,'intval'); 
        $grade_id = $this->request->get("grade_id",0,'intval'); 
        $subject_id = $this->request->get("subject_id",0,'intval'); 
        $version_id = $this->request->get("version_id",0,'intval'); 
        $kw = $this->request->get("kw",'','trim'); 
		if(!empty($grade_id)){
			$map['grade_id'] = $grade_id;
		}
		if(!empty($subject_id)){
			$map['subject_id'] = $subject_id;
		}
		if(!empty($version_id)){
			$map['version_id'] = $version_id;
		}
		if(!empty($kw)){
			$map['title'] = ['like',"%".$kw."%"];
		}
		
		$map['status'] = 1;
        $count = Db::name("course")->where($map)->count();
		$list = Db::name("course")->where($map)->field('id,title,price,study_nums,photo')->order('id desc')
			->page($page,$pagesize)->select();  
			
		$list = array_map(function ($rs){
            $rs["less_nums"] = Db::name("course_less")->where('course_id',$rs['id'])->count();
			return $rs;
        }, $list);		
			
        $pages = ceil($count/$pagesize);
        $data = array('list'=>$list,'count'=>$count,'pages'=>$pages,'pagesize'=>$pagesize);
		return $this->show(1,"成功", $data);
		
    }
    
    
    public function detail()
    {
        
        $id = $this->request->get("id",0,'intval'); 
        $user_id = $this->user_id;
        if(empty($id)){
            return $this->show(2001,"缺少id!", '');
        }
        $info = Db::name("course")->field('id,title,price,photo,teacher,teacher_head,teacher_desc,info,study_nums,subject_id')->where('id',$id)->where('status',1)->find();
        if(empty($info)){
            return $this->show(2001,"课程不存在或已下架!", '');
        }
		$info["less_nums"] = Db::name("course_less")->where('course_id',$info['id'])->count();
		$info['is_collect'] = 0;
		if(!empty($user_id)){
			$flag = Db::name("course_collect")->where('user_id',$user_id)->where('course_id',$id)->value('id');
			if(!empty($flag)){
				$info['is_collect'] = 1;
			}
		}
		$zhangjie = Db::name("course_unit")->field('id,title')->where('course_id',$id)->where('status',1)->order('sort asc,id asc')->select();
		foreach($zhangjie as $k=>$v){
			$zhangjie[$k]['less_list'] = Db::name("course_less")->field('id,title,file_url')->where('unit_id',$v['id'])->where('status',1)->order('sort asc,id asc')->select();
		}
		$info['zhangjie'] = $zhangjie;
		$info['is_play'] = 0;
		if($info['price']==0){
			$info['is_play'] = 1;
		}else{
			$buyed = Db::name("course_order")->where('course_id',$info['id'])->where('user_id',$user_id)->where('type',1)->where('status',2)->value('id');
			$info['is_play'] = !empty($buyed)?1:0;
		}
		
		$hot_list = Db::name("course")->field('id,title,photo,study_nums')->where('status',1)->order('study_nums desc')->limit(10)->select();
		$info["hot_list"] = array_map(function ($rs){
            $rs["less_nums"] = Db::name("course_less")->where('course_id',$rs['id'])->count();
			return $rs;
        }, $hot_list);	
		
		
		$link_list = Db::name("course")->field('id,title,photo,study_nums')->where('id','<>',$info['id'])->where('subject_id',$info['subject_id'])->where('status',1)->order('study_nums desc')->limit(21)->select();
		$info["link_list"] = array_map(function ($rs){
            $rs["less_nums"] = Db::name("course_less")->where('course_id',$rs['id'])->count();
			return $rs;
        }, $link_list);
		Db::name("course")->where('id',$id)->setInc('study_nums');
		
		return $this->show(1,"success", $info);
		
    }
	
	//下单购买
    public function submit_order()
    {
        $user_id = $this->user_id;
        if($this->request->isPost()){
			//$data = $this->request->post();
			$data['course_id'] = $this->request->post("course_id",0);
			if( empty($data['course_id'])){
				return $this->show(2001,"缺少course_id", '');
			}
			$course = Db::name("course")->where('id',$data['course_id'])->find();
			if(empty($course)){
				return $this->show(2001,"课程不存在", '');
			}
			if($course['price']<=0){
				return $this->show(2001,"免费课程无需购买", '');
			}
			Db::name("course_order")->where('course_id',$data['course_id'])->where('user_id',$user_id)->where('type',1)->where('status',1)->delete();
			$course_order = Db::name("course_order")->where('course_id',$data['course_id'])->where('user_id',$user_id)->where('type',1)->where('status',2)->find();
			if(!empty($course_order)){
				return $this->show(2001,"已经购买过了", '');
			}
			
			$data['user_id'] = $user_id;
			$data['order_sn'] = $this->build_order_no();//生成唯一订单号
			$data['order_money'] = $course['price'];
			$data['course_title'] = $course['title'];
			$data['status'] = 1;
			$data['type'] = 1;
			$data['create_time'] = time();
			Db::startTrans();
			$flag = Db::name("course_order")->strict(false)->insertGetId($data);
				
			if($flag){
				$data_return = ['order_id'=>$flag,'order_sn'=>$data['order_sn']];
				Db::commit();
				return $this->show(1,"success", $data_return);
			}else{
				Db::rollback();
				return $this->show(2002, "fail", "");
			}
		}else{
            return $this->show(2001,"非法请求", '');
        }	
    }
    
}